{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b3a6ae7-59e0-4498-814e-a93e1d634cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "%use fuel(2.3.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ab39a624-cec1-4102-b23d-640c312b72dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "val envs = java.io.File(\"../../.env\")\n",
    "    .readLines()\n",
    "    .map {\n",
    "        it.split(\"=\")[0] to it.split(\"=\")[1].trim('\"')\n",
    "    }.toMap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "575b8a31-6e1c-4edc-98f8-72c52992797b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val session = envs.get(\"AOC_SESSION\")\n",
    "val year = 2021\n",
    "val day = 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd799fac-9c82-4c5a-b308-60691a01a866",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun getInput(year: Int, day: Int, session: String): String {\n",
    "    val (_, _, result) = \"https://adventofcode.com/$year/day/$day/input\"\n",
    "    .httpGet()\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get().trim()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cac8a111-9146-49ea-8989-c6392869eaaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun submitAnswer(year: Int, day: Int, session: String, level: Int, answer: String): String {\n",
    "    val (_, _, result) = Fuel\n",
    "    .post(\n",
    "        \"https://adventofcode.com/$year/day/$day/answer\", \n",
    "        parameters = listOf(\"level\" to level, \"answer\" to answer))\n",
    "    .header(\"cookie\" to \"session=$session\")\n",
    "    .responseString()\n",
    "        \n",
    "    return result.get()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a064468c-9c51-4307-ba19-e3ff83c6c4ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "val sample = \"\"\"6,10\n",
    "0,14\n",
    "9,10\n",
    "0,3\n",
    "10,4\n",
    "4,11\n",
    "6,0\n",
    "6,12\n",
    "4,1\n",
    "0,13\n",
    "10,12\n",
    "3,4\n",
    "3,0\n",
    "8,4\n",
    "1,10\n",
    "2,14\n",
    "8,10\n",
    "9,0\n",
    "\n",
    "fold along y=7\n",
    "fold along x=5\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "878489a4-5e1e-4c04-a209-650185a8de67",
   "metadata": {},
   "outputs": [],
   "source": [
    "val input = getInput(year, day, session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "07df67e5-7a24-42e8-a6c4-f555675939df",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partOne(input: String): String {\n",
    "    var s = mutableSetOf<Pair<Int, Int>>()\n",
    "    \n",
    "    for (line in input.split('\\n').filter { it.length > 0 }) {\n",
    "        if (line[0] == 'f') {\n",
    "            val m = line.split('=')[1].toInt()\n",
    "            val ns = mutableSetOf<Pair<Int, Int>>()\n",
    "            if (line[11] == 'x') {\n",
    "                for ((x, y) in s) {\n",
    "                    ns.add(Pair(if (x < m) x else m * 2 - x, y))\n",
    "                }\n",
    "            } else {\n",
    "                for ((x, y) in s) {\n",
    "                    ns.add(Pair(x, if (y < m) y else m * 2 - y))\n",
    "                }\n",
    "            }\n",
    "            return ns.size.toString()\n",
    "        } else {\n",
    "            val (x, y) = line.split(',').map(String::toInt)\n",
    "            s.add(Pair(x, y))\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    return \"\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eaa273a8-a166-44e0-9023-2449fecbbb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partOne(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2ad99e07-5a96-40c4-873c-e72dd7c5f499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "684"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val partOneAns = partOne(input)\n",
    "partOneAns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23679ce7-8258-41ae-9813-dff5745b482c",
   "metadata": {},
   "outputs": [],
   "source": [
    "submitAnswer(year, day, session, 1, partOneAns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "528387cf-abb5-4da2-9fd3-9bbf865444a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fun partTwo(input: String) {\n",
    "    var s = mutableSetOf<Pair<Int, Int>>()\n",
    "    \n",
    "    for (line in input.split('\\n').filter { it.length > 0 }) {\n",
    "        if (line[0] == 'f') {\n",
    "            val m = line.split('=')[1].toInt()\n",
    "            val ns = mutableSetOf<Pair<Int, Int>>()\n",
    "            if (line[11] == 'x') {\n",
    "                for ((x, y) in s) {\n",
    "                    ns.add(Pair(if (x < m) x else m * 2 - x, y))\n",
    "                }\n",
    "            } else {\n",
    "                for ((x, y) in s) {\n",
    "                    ns.add(Pair(x, if (y < m) y else m * 2 - y))\n",
    "                }\n",
    "            }\n",
    "            s = ns\n",
    "        } else {\n",
    "            val (x, y) = line.split(',').map(String::toInt)\n",
    "            s.add(Pair(x, y))\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    val maxX = s.map { it.first }.maxOrNull()!!\n",
    "    val maxY = s.map { it.second }.maxOrNull()!!\n",
    "    \n",
    "    for (y in 0..maxY) {\n",
    "        for (x in 0..maxX) {\n",
    "            print(if (Pair(x, y) in s) '#' else '.')\n",
    "        }\n",
    "        println()\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b12a86b5-8d6f-4a6c-851a-af1d84ae168d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#####\n",
      "#...#\n",
      "#...#\n",
      "#...#\n",
      "#####\n"
     ]
    }
   ],
   "source": [
    "partTwo(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "62a5e94e-550f-42fe-9597-32e6f87a5b1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..##.###..####.###..#.....##..#..#.#..#\n",
      "...#.#..#....#.#..#.#....#..#.#.#..#..#\n",
      "...#.#..#...#..###..#....#....##...####\n",
      "...#.###...#...#..#.#....#.##.#.#..#..#\n",
      "#..#.#.#..#....#..#.#....#..#.#.#..#..#\n",
      ".##..#..#.####.###..####..###.#..#.#..#\n"
     ]
    }
   ],
   "source": [
    "partTwo(input)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.6.20-dev-5432"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
